<?php
require_once 'application/library/db/gcDaoFactory.php';
require_once 'application/library/helper/amountHelper.php';
require_once 'application/constant/daoConfig.php';

/**
 * 库存警报一览Service
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class stock_services_stockAlertListService {

    private $objDaoFactory;

    public function __construct() {
        $this->objDaoFactory = gcDaoFactory::getInstance();
    }

    /**
     * 库存情报一览件数取得
     * pbGetSearchCount
     * @param   array   $arrCondition
     * @return  array   $arrReturn
     */
    public function pbGetSearchCount($arrCondition) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $count = $objGoodsDao->pbGetSearchCount($arrCondition);
        return $count;
    }

    /**
     * 库存情报
     * pbSearchGoods
     * @param   array   $arrCondition
     * @return  array   $arrReturn
     */
    public function pbSearchGoods($arrCondition) {
        $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
        $arrGoods = $objGoodsDao->pbSearchGoods($arrCondition);
        $intAlert = 0;
        foreach($arrGoods as $key => &$value) {
            $value["SIMVARIATION"] = mb_substr($value["VARIATION"], 0, 10, "utf-8");
            if($value["SUPPLIERNUM"] == null) {
                $value["SUPPLIERNUM"] = 0;
            }
            if ($value["STOCK_NUM"] < $value["STOCK_MIN"]) {
                $value["SUGGEST"] = ceil($value["STOCK_MIN"] + ($value["STOCK_MAX"] - $value["STOCK_MIN"])/2);
                if ($value["SUPPLIERNUM"]>0) {
                    $value["Checkfg"] = 1;
                } else {
                    $value["Checkfg"] = 2;
                }
                $intAlert++;
            } else if ($value["STOCK_NUM"] > $value["STOCK_MAX"]) {
                $value["SUGGEST"] = "-".($value["STOCK_NUM"] - $value["STOCK_MAX"]);
            } else {
                $value["SUGGEST"] = 0;
            }
        }
        return $arrGoods;
    }

    /**
     * 库存情报
     * pbSearchGoods
     * @param   array   $arrCondition
     * @return  array   $arrReturn
     */
    public function pbGoodsStockAlertUpdate($arrParameter) {
        try {
            $objGoodsDao = $this->objDaoFactory->createDao(daoConfig::T_EC_M_GOODS_DAO);
            //事务
            $objDb = $objGoodsDao->beginTransaction();
            for ($i = 0; $i < count($arrParameter["Id"]); $i++) {
                $arrTemp = array(
                    "Id" => $arrParameter["Id"][$i],
                    "Ver" => $arrParameter["Ver"][$i],
                    "Suggest" => $arrParameter["Suggest"][$i],
                    "Min" => $arrParameter["Min"][$i],
                    "Max" => $arrParameter["Max"][$i],
                );
                $intCount = $objGoodsDao->pbGoodsStockAlertUpdate($arrTemp);
                if ($intCount == 0) {
                    $objGoodsDao->rollBack();
                    return $intCount;
                }
            }
            return 1;
        } catch (Exception $e) {
            $objGoodsDao->rollBack();
            throw $e;
        }
    }
}   